package com.puhui.lib.widgets

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import com.puhui.lib.R

class NetworkStatusView @JvmOverloads constructor(
    context: Context, attributeSet: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attributeSet, defStyleAttr) {

    private var totalViewClickListener: OnClickListener? = null
    private var isLoading = false

    init {
        initView(context, attributeSet)
    }

    private fun initView(context: Context, attributeSet: AttributeSet?) {
        val typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.NetworkStatusView)
        val isLoadInCenter = typedArray.getBoolean(R.styleable.NetworkStatusView_isLoadingInCenter, false)
        typedArray.recycle()

        LayoutInflater.from(context).inflate(R.layout.network_status_layout, this)
    }
//        retryTv.setClick {
//            totalViewClickListener?.apply {
//                startLoading()
//                onClick(this@NetworkStatusView)
//            }
//        }
//
//        loadingImg.visibility = GONE
//        if (isLoadInCenter) {
////            showInCenter()
//            val layoutParams = loadingImg.layoutParams as LayoutParams
//            layoutParams.topMargin = 0
//            layoutParams.addRule(CENTER_IN_PARENT)
//
////            val layoutParams1 = loadingIndicatorView.layoutParams as LayoutParams
////            layoutParams1.topMargin = 0
////            layoutParams1.addRule(CENTER_IN_PARENT)
//        }
//    }
//
//    /**
//     * 设置gif所显示的位置
//     */
//    fun setLoadingLocation(isLoadingInCenter: Boolean) {
//        if (isLoadingInCenter) {
//            val layoutParams = loadingImg.layoutParams as LayoutParams
//            layoutParams.topMargin = 0
//            layoutParams.addRule(CENTER_IN_PARENT)
//        } else {
//            val layoutParams = loadingImg.layoutParams as LayoutParams
//            layoutParams.topMargin = SizeUtils.dp2px(120f)
//            layoutParams.addRule(ALIGN_PARENT_TOP)
//        }
//    }
//
//    override fun setOnClickListener(l: OnClickListener?) {
//        totalViewClickListener = l
//
//        //只处理R.id.retry_tv的点击事件
//        // super.setOnClickListener(totalViewClickListener);
//    }
//
//    fun setNetErrorImg(@DrawableRes resourceId: Int): NetworkStatusView {
//        netErrorImg.setImageResource(resourceId)
//        return this
//    }
//
//    fun setNetWorkErrorTip(tip: String): NetworkStatusView {
//        networkErrorTip.text = tip
//        return this
//    }
//
//    fun startLoading() {
//        if (!isLoading) {
//            isLoading = true
//            visibility = View.VISIBLE
////            loadingIndicatorView.visibility = View.VISIBLE
//            networkErrorLayout.visibility = View.GONE
//
//            loadingImg.visibility = View.VISIBLE
//            loadingImg.setImageResource(R.drawable.loading)
//            // loadingImg.setImageResource(R.drawable.loading_small);
//        }
//    }
//
//    fun stopLoading() {
//        if (isLoading) {
//            isLoading = false
//            visibility = View.GONE
////            loadingIndicatorView.visibility = View.GONE
//            networkErrorLayout.visibility = View.GONE
//
//            loadingImg.visibility = View.GONE
//            loadingImg.setImageBitmap(null)
//        }
//    }
//
//    /**
//     * 该方法可在初始化之后里面调用
//     */
//    private fun showInCenter() {
//        loadingImg.viewTreeObserver.addOnGlobalLayoutListener(object :
//            ViewTreeObserver.OnGlobalLayoutListener {
//            override fun onGlobalLayout() {
//                loadingImg.viewTreeObserver.removeOnGlobalLayoutListener(this)
//                val topMargin = (measuredHeight - loadingImg.measuredHeight) / 2
//                val layoutParams = loadingImg.layoutParams as MarginLayoutParams
//                layoutParams.topMargin = topMargin
//                loadingImg.layoutParams = layoutParams
//            }
//        })
//    }
//
//    override fun setVisibility(visibility: Int) {
//        super.setVisibility(visibility)
//        networkErrorLayout.visibility = visibility
//        loadingImg.visibility = View.GONE
//    }
//
//    fun onNetworkError() {
//        isLoading = false
//        visibility = View.VISIBLE
//        loadingImg.visibility = View.GONE
//        loadingImg.setImageBitmap(null)
//        networkErrorLayout.visibility = View.VISIBLE
//    }
//
//    fun isVisible(): Boolean {
//        return visibility == View.VISIBLE
//    }
//
//    fun onNetworkSuccess() {
//        stopLoading()
//    }
}